package com.onstar.cn.framework.network;

import java.lang.reflect.Method;
import java.util.Locale;

import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;

/**
 * Http request inject utils.
 *
 * @author 7828wc
 */
public class HttpInjectUtils {
    private static String DEFAULT_METHOD = "httpRequest";

    /**
     * Http request warp<br/>
     * parameter HttpRequest must contain
     *
     * @param clazz
     * @param methodName
     * @param parameterTypes
     */
    public static RequestEntity injectHttp(Class<?> clazz, String methodName, RequestEntity request) {
//		if (request == null)
//			request = new RequestEntity();
//		try {
//			Method method = clazz.getMethod(methodName);
//			request.setLanguage(DeviceUtil.getLanguage());
//			Request requestAnnotation = method.getAnnotation(Request.class);
//			if (requestAnnotation != null) {
//				HeadsValue headsAnnotation = requestAnnotation.heads();
//				if (headsAnnotation != null) {
//					request.setAccept(headsAnnotation.accept().value());
//					if (headsAnnotation.authorization() != null && !"".equals(headsAnnotation.authorization())) {
//						request.setAuthorization(headsAnnotation.authorization());
//					}
//					request.setLanguage(headsAnnotation.language().value());
//				}
//				request.setMethod(requestAnnotation.method().value());
//			}
//			if(Locale.US.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					||Locale.ENGLISH.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())) {
//				request.setLanguage(Language.EN.value());
//			}else if(Locale.CHINA.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					||Locale.CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					||Locale.PRC.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					||Locale.TAIWAN.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					||Locale.TRADITIONAL_CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
//					) {
//				request.setLanguage(Language.ZH.value());
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return request;
        return injectHttp(clazz, methodName, request, Object.class);
    }

    /**
     * Http request warp<br/>
     * parameter HttpRequest must contain
     *
     * @param clazz
     * @param methodName
     * @param request
     * @param paramClazz 第一个参数不能是Object.class
     * @return
     */
    public static RequestEntity injectHttp(Class<?> clazz, String methodName, RequestEntity request, Class<?>... paramClazz) {
        if (request == null)
            request = new RequestEntity();
        try {
            Method method = null;
            if (paramClazz != null && paramClazz.length > 0 && !paramClazz[0].equals(Object.class)) {
                method = clazz.getMethod(methodName, paramClazz);
            } else {
                method = clazz.getMethod(methodName);
            }
            Request requestAnnotation = method.getAnnotation(Request.class);
            if (requestAnnotation != null) {
                HeadsValue headsAnnotation = requestAnnotation.heads();
                if (headsAnnotation != null) {
                    request.setAccept(headsAnnotation.accept().value());
                    if (headsAnnotation.authorization() != null && !"".equals(headsAnnotation.authorization())) {
                        request.setAuthorization(headsAnnotation.authorization());
                    }
                    request.setLanguage(headsAnnotation.language().value());
                }
                request.setMethod(requestAnnotation.method().value());
            }

            if (request.getLanguage().equalsIgnoreCase(Language.AUTO.value())) {
                if (Locale.US.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                        || Locale.ENGLISH.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())) {
                    request.setLanguage(Language.EN.value());
                } else if (Locale.CHINA.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                        || Locale.CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                        || Locale.PRC.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                        || Locale.TAIWAN.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                        || Locale.TRADITIONAL_CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())) {
                    request.setLanguage(Language.ZH.value());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;
    }

    public static RequestEntity getDefaultConfig(Language language, HttpMethod method, HttpAccept accept, RequestEntity request) {
        if (request == null)
            request = new RequestEntity();
        if (Locale.US.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.ENGLISH.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())) {
            request.setLanguage(Language.EN.value());
        } else if (Locale.CHINA.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.PRC.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.TAIWAN.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())
                || Locale.TRADITIONAL_CHINESE.getLanguage().equalsIgnoreCase(DeviceUtil.getLanguage())) {
            request.setLanguage(Language.ZH.value());
        }
        request.setMethod(method.value());
        request.setAccept(accept.value());

        return request;
    }

    /**
     * Http request warp<br/>
     * parameter HttpRequest must contain
     *
     * @param clazz
     * @param methodName
     * @param parameterTypes
     */
    public static RequestEntity injectHttp(Class<?> clazz, RequestEntity request) {
        return injectHttp(clazz, DEFAULT_METHOD, request);
    }
}
